/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package algrt;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Damn
 */
public class ImMatr {


    public int [] abrirImagen(String ruta)throws IOException {
        try{
            BufferedImage bfI=ImageIO.read(new File(ruta));
            int w=bfI.getWidth();
            int h=bfI.getHeight();
            int [] tRgb= new int[w*h];

            int [] sRGB=bfI.getRGB(0, 0, w, h, tRgb, 0   ,w);
            //System.out.println("w: "+w+" - h: "+h);
            //verificar nulos
            dilate(sRGB, w,1);
           // muestraRGB(sRGB,w);
            return sRGB;

        }catch (Exception ex){
            System.out.println("ERr: "+ex.getCause());
        }
       return null;
}

    public void muestraRGB(int [] mRGB,int ancho){
        try{
            System.out.println("Tamano: "+mRGB.length);

            for (int i=0;i<mRGB.length;i++){
                System.out.print(qColor(mRGB[i]));
                if (i>=ancho && (i % ancho==0))
                    System.out.print("\n");
            }

             System.out.println("\n");
        }catch(Exception ex){
            System.out.println("ERr:"+ex.getMessage());
        }

    }

    public String qColor(int c){
       
        int  r = (c & 0x00ff0000) >> 16;
        int  g = (c & 0x0000ff00) >> 8;
        int  b = c & 0x000000ff;
/*
        if (r==255 && g==255 && b==255){
            return " ";
        }else{
            return "X";
        }*/
        //return "("+r+","+g+","+b+")";
        return c+" ";
    }


    public int [] dilate(int [] mRGB,int ancho, int nDilat){
        int fin=mRGB.length;
        int i=0;
        boolean engrosado=false;
        if (nDilat==1){
            while (i<fin){
                engrosado=false;
               // if (i>=ancho && (i % ancho==0)) i++;
                
                if (mRGB[i]<Color.white.getRGB()){//si hay trazo cualquiera
                    mRGB[i]=Color.black.getRGB();
                   // if (mRGB[i+1]==Color.white.getRGB())
                      //  mRGB[i+1]=Color.black.getRGB();//engrosa horizontal
                    //if (mRGB[i+2]==Color.white.getRGB())
                    //    mRGB[i+2]=Color.black.getRGB();
                    //engrosado vertical
                   /* if (i-ancho>=0){
                            mRGB[i-ancho]=Color.black.getRGB();
                            mRGB[i-ancho+1]=Color.black.getRGB();
                            mRGB[i-ancho-1]=Color.black.getRGB();
                    }
*/
                    
                    //engrosado=true;
                }
                i+=1;
               // i+=engrosado?2:1;//para recorrer cada celda de arreglo de pixeles
            }
        }
        if (nDilat==2){//color rojo 255,0,0
            while (i<fin){
                if (mRGB[i]<Color.white.getRGB()){//si hay trazo cualquiera
                    mRGB[i+1]=mRGB[i];//engrosa
                    i++;
                }
                //if (i>=ancho && (i % ancho==0)) i++;borde
                i++;//para recorrer cada celda de arreglo de pixeles
            }
        }
        return mRGB;
    }


}
